﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _7.装饰器模式
{
    class Program
    {
        static void Main(string[] args)
        {
            Phone ap = new ApplePhone(); //新买了个苹果手机
            Decorator aps = new Sticker(ap); //准备贴膜组件
            aps.Print();

            Decorator apa = new Accessories(ap); //过了几天新增了挂件组件
            apa.Print();

            Sticker s = new Sticker(ap);       //准备贴膜组件
            Accessories a = new Accessories(s);//同时准备挂件
            a.Print();

        }
    }

    public abstract class Phone
    {
        public abstract void Print();
    }
    public class ApplePhone : Phone
    {
        public override void Print()
        {
            Console.WriteLine("我有一部苹果手机");
        }
    }
    public abstract class Decorator : Phone
    {
        private Phone p;    //该装饰对象装饰到的Phone组件实体对象 
        public Decorator(Phone p)
        {
            this.p = p;
        }
        public override void Print()
        {
            if (this.p != null)
            {
                p.Print();
            }
        }
    }
    public class Sticker : Decorator// 贴膜
    {
        public Sticker(Phone p) : base(p) { }
        public override void Print()
        {
            base.Print();
            AddSticker();
        }
        public void AddSticker()
        {
            Console.WriteLine("现在苹果手机有贴膜了");
        }
    }
    public class Accessories : Decorator// 手机挂件，即具体装饰者
    {
        public Accessories(Phone p) : base(p) { }
        public override void Print()
        {
            base.Print();
            AddAccessories();
        }
        public void AddAccessories()
        {
            Console.WriteLine("现在苹果手机有漂亮的挂件了");
        }
    }

}
